#include "TransformHelper.h"
#include <cmath>


QPointF TransformHelper::transform (const QPoint & point, const QMatrix3x3 & mat)
{
  QMatrix1x3 p = mat * toHomogeneousPoint (point);
  return QPointF (p (0, 0) / p (2, 0), p (1, 0) / p (2, 0));
}



QMatrix1x3 TransformHelper::toHomogeneousPoint (const QPoint & point)
{
  QMatrix1x3 homogeneousPoint;
  homogeneousPoint (0, 0) = point.x ();
  homogeneousPoint (1, 0) = point.y ();
  homogeneousPoint (2, 0) = 1.0;
  return homogeneousPoint;
}



QPoint TransformHelper::toNormalPoint (const QMatrix1x3 & point)
{
  int x = point (0, 0) / point (2, 0);
  int y = point (1, 0) / point (2, 0);
  return QPoint (x, y);
}



QMatrix3x3 TransformHelper::getTranslateMatrix (QPoint v)
{
  QMatrix3x3 translateMatrix;
  translateMatrix (0, 2) = v.x ();
  translateMatrix (1, 2) = v.y ();
  return translateMatrix;
}



QMatrix3x3 TransformHelper::getRotateMatrix (float angleR)
{
  QMatrix3x3 rotateMatrix;
  rotateMatrix (0, 0) = cos (angleR);
  rotateMatrix (0, 1) = -sin (angleR);
  rotateMatrix (1, 0) = sin (angleR);
  rotateMatrix (1, 1) = cos (angleR);
  return rotateMatrix;
}



QMatrix3x3 TransformHelper::getScaleMatrix (QPointF val)
{
  QMatrix3x3 scaleMatrix;
  scaleMatrix (0, 0) = val.x ();
  scaleMatrix (1, 1) = val.y ();
  return scaleMatrix;
}

QRect TransformHelper::getPolygonBounding (const QPolygon & polygon)
{
  QRect bounding = polygon.boundingRect ();
  bounding.setWidth (bounding.width () - 1);
  bounding.setHeight (bounding.height () - 1);
  return bounding;
}



